﻿angular.module('Caltech').controller('aeItemController', function ($scope, Restangular, $state, ngTableParams, $stateParams, $location, Constants, Roles, toaster, Reasons, $filter) {
    'use strict';

    $scope.regions = [{
        value: 'SG',
        text: 'SG'
    }, {
        value: 'BN',
        text: 'BN'
    }];

    var defaultModel = {
        company: {},
        aeDate: new Date(),
        aeNo: '',
        poNo: '',
        user: {
            fullName: Constants.accountInfo.fullName,
            roleName: Constants.accountInfo.roleName
        },
        reason: '',
        total: 0,
        totalMoney: '',
        paymentDate: new Date(),
        note: '',
        createdBy: Constants.accountInfo.id,
        updatedBy: Constants.accountInfo.id
    };


    $scope.roles = {};

    $scope.reasons = Reasons;

    $scope.selectedReason = Reasons[0];

    $scope.checkAuthen = function () {
        $scope.roles = {
            leader: $scope.model.leader != null ? true : false,
            accounting: $scope.model.accounting != null ? true : false,
            accountingManager: $scope.model.accountingManager != null ? true : false,
            manager: $scope.model.manager != null ? true : false
        };
    };

    $scope.findReason = function (id) {
        $scope.selectedReason = $filter('filter')(Reasons, { value: id })[0];
    };

    if ($stateParams.id) {
        Restangular.one('ae', $stateParams.id).get().then(function (model) {
            if (model) {
                $scope.model = model;
                $scope.findReason($scope.model.reason);
                $scope.checkAuthen();
            }
        });
    } else if ($stateParams.model) {
        // it's copy mode
        $scope.model = JSON.parse($stateParams.model);
        $scope.checkAuthen();
    } else {
        $scope.model = defaultModel;
        $scope.checkAuthen();
    }

    $scope.getCustomerList = function (name) {
        return Restangular.all('customer').getList({
            name: name
        });
    };

    $scope.updateCustomer = function (customer) {
        if (!customer) {
            $scope.model.company = {};
        } else if (typeof (customer) === 'object') {
            $scope.model.company = customer;
        }
    };

    $scope.save = function (model) {
        model.companyId = model.company.id;
        model.company = undefined;
        model.reason = $scope.selectedReason.value;

        model.createdBy = Constants.accountInfo.id;
        model.user = undefined;
        model.leader = undefined;
        model.accounting = undefined;
        model.accountingManager = undefined;
        model.manager = undefined;
        return Restangular.all('ae').post(model).then(function (id) {
            $state.go('ae-item', { id: id });
        });
    };

    $scope.copy = function () {
        var model = Restangular.stripRestangular($scope.model);

        toaster.pop('info', "Info", 'Prepared a copy version ');

        model.id = undefined;
        model.user = Constants.accountInfo;
        model.reason = $scope.selectedReason.value;

        model.leader = undefined;
        model.accounting = undefined;
        model.accountingManager = undefined;
        model.manager = undefined;

        $state.go('ae-new', {
            model: JSON.stringify(model)
        });
    };


    $scope.remove = function (model) {
        return model.remove().then(function () {
            history.back();
        });
    };

    $scope.update = function (model) {
        model.companyId = model.company.id;
        model.company = undefined;
        model.reason = $scope.selectedReason.value;

        model.updatedBy = Constants.accountInfo.id;
        model.user = undefined;
        model.leader = undefined;
        model.accounting = undefined;
        model.accountingManager = undefined;
        model.manager = undefined;

        return model.put().then(function () {
            $state.forceReload();
        });
    };



    $scope.change = function (role) {
        if (role == 1) {
            if ($scope.roles.leader) {
                $scope.model.leaderId = Constants.accountInfo.id;
                $scope.model.leader = Constants.accountInfo;
            }
            else {
                $scope.model.leaderId = null;
                $scope.model.leader = null;
            }
        } else if (role == 2) {
            if ($scope.roles.accounting) {
                $scope.model.accountingId = Constants.accountInfo.id;
                $scope.model.accounting = Constants.accountInfo;
            }
            else {
                $scope.model.accountingId = null;
                $scope.model.accounting = null;
            }
        } else if (role == 3) {
            if ($scope.roles.accountingManager) {
                $scope.model.accountingManagerId = Constants.accountInfo.id;
                $scope.model.accountingManager = Constants.accountInfo;
            }
            else {
                $scope.model.accountingManagerId = null;
                $scope.model.accountingManager = null;
            }

        } else if (role == 4) {
            if ($scope.roles.manager) {
                $scope.model.managerId = Constants.accountInfo.id;
                $scope.model.manager = Constants.accountInfo;
            }
            else {
                $scope.model.managerId = null;
                $scope.model.manager = null;
            }
        }
    };
});